<?php
/**
 * avatar.php
 *
 * @copyright 2018 opencart.cn
 *
 * All Rights Reserved
 * @link http://guangdawangluo.com
 *
 * @author StifferChen <chenlin@opencart.cn>
 * @created 2018-05-09 21:33
 * @modified 2018-05-09 21:33
 */
class ControllerAccountAvatar extends Controller
{
    public function __construct($registry)
    {
        parent::__construct($registry);
        $this->permission();
        $this->load->language('account/avatar');
    }

    public function index()
    {
        $this->load->language('account/avatar');
        $this->document->setTitle($this->language->get('heading_title'));

        $data['breadcrumbs'] = array();
        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/home')
        );
        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_account'),
            'href' => $this->url->link('account/account', '', true)
        );
        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('heading_title'),
            'href' => $this->url->link('account/avatar', '', true)
        );

        $avatar = $this->customer->getAvatar();

        $this->load->model('tool/image');
        $data['avatar'] = $this->model_tool_image->resize('no_image.png', 100, 100);
        $data['avatar_url'] = '';
        if (is_file(DIR_IMAGE . $avatar)) {
            $data['avatar'] = $this->model_tool_image->resize($avatar, 100, 100);
            $data['avatar_url'] = $avatar;
        }

        $data['column_left'] = $this->load->controller('common/column_left');
        $data['column_right'] = $this->load->controller('common/column_right');
        $data['content_top'] = $this->load->controller('common/content_top');
        $data['content_bottom'] = $this->load->controller('common/content_bottom');
        $data['footer'] = $this->load->controller('common/footer');
        $data['header'] = $this->load->controller('common/header');
        $data['action'] = $this->url->link('account/avatar/update');
        $this->response->setOutput($this->load->view('account/avatar', $data));
    }

    public function update()
    {
        $this->permission();
        $avatar = isset($this->request->post['avatar_url']) ? $this->request->post['avatar_url'] : '';
        if ($avatar && is_file(DIR_IMAGE . $avatar)) {
            $this->load->model('account/customer');
            $this->model_account_customer->updateAvatar($avatar);
            $this->session->data['success'] = $this->trans('text_success');
        }
        $this->response->redirect(uriLink('account/account'));
    }

    public function upload()
    {
        $this->permission();
        $json = array();
        $file = $_FILES ? $_FILES[array_keys($_FILES)[0]] : array();
        if (!$file) {
            $json = array(
                'code' => 0,
                'msg' => $this->trans('error_empty_upload')
            );
        } else {
            if (!checkExtensions('fileinfo')) {
                $json = array(
                    'code' => 0,
                    'msg' => $this->trans('error_not_extension')
                );
            } else {
                $tmp_file = $file['tmp_name'];
                $arrow_type = array('image/png', 'image/jpeg', 'image/jpg');
                if (!checkFileType($tmp_file, $arrow_type)) {
                    $json = array(
                        'code' => 0,
                        'msg' => $this->trans('error_file_type')
                    );
                } else {
                    $file_size = $file['size'];
                    if ($file_size > UPLOAD_SIZE_LIMIT) {
                        $json = array(
                            'code' => 0,
                            'msg' => $this->trans('error_file_size')
                        );
                    } else {
                        $input_path = 'customer/customer_' . $this->customer->getId() . '/avatar';
                        $file_path = 'catalog/' . $input_path;
                        $path = DIR_IMAGE . $file_path;
                        checkPath($path);
                        $file_prefix = 'avatar_' . time() . str_replace(' ', '', str_replace('.', '', microtime()));
                        $file_name_arr = explode('.', $file['name']);
                        $file_suffix = $file_name_arr[count($file_name_arr) - 1];
                        $file_name = $file_prefix . '.' . $file_suffix;
                        $return_path = $file_path . '/' . $file_name;
                        @move_uploaded_file($tmp_file, $path . '/' . $file_name);
                        if (is_file($path . '/' . $file_name)) {
                            $json = array(
                                'code' => 1,
                                'image' => resizeImage($file_path . '/' . $file_name),
                                'url' => $return_path
                            );
                        } else {
                            $json = array(
                                'code' => 0,
                                'msg' => $this->trans('error_remove')
                            );
                        }
                    }
                }
            }
        }
        $this->response->addHeader('Content-Type: application/json');
        $this->response->setOutput(json_encode($json));
    }

    private function permission()
    {
        if (!$this->customer->isLogged()) {
            die('permission dined!');
        }
    }

    private function trans($text)
    {
        return $this->language->get($text);
    }

}